/*
Copyright  2018 Blockchain Technology and Application Joint Lab, Fintech Research Center of ISCAS.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
 */
import {Mongo} from 'meteor/mongo';
import {SimpleSchema} from 'meteor/aldeed:simple-schema';
//define User fields
class UserCertCollection extends Mongo.Collection {
    insert(doc, callback) {
        const ourDoc = doc;
        const result = super.insert(ourDoc, callback);
        return result;
    }

    update(selector, modifier) {
        const result = super.update(selector, modifier);
        return result;
    }

    remove(selector) {
        const result = super.remove(selector);
        return result;
    }
}

export const UserCert = new UserCertCollection('user_cert');

// Deny all client-side updates since we will be using methods to manage this collection
UserCert.deny({
    insert() {
        return true;
    },
    update() {
        return true;
    },
    remove() {
        return true;
    },
});

UserCert.schema = new SimpleSchema({
    user_tel: {
        type: String,
        max: 100,
    },
    serial_number: {
        type: String,
        max: 100,
    },
    signature_algorithm: {
        type: String,
        max: 100,
    },
    issuer: {
        type: String,
    },
    not_before: {
        type: String,
    },
    not_after: {
        type: String,
    },
    subject: {
        type: String,
    },
    key_algorithm: {
        type: String,
        max: 100,
    },
    signature: {
        type: String,
    },
});

UserCert.attachSchema(UserCert.schema);

UserCert.publicFields = {
    user_tel: 1,
    serial_number: 1,
    signature_algorithm: 1,
    issuer: 1,
    not_before: 1,
    not_after: 1,
    subject: 1,
    key_algorithm: 1,
    signature: 1,
};

if (Meteor.isServer) {
    // This code only runs on the server
    Meteor.publish('user_cert', function (user) {
            let result = UserCert.find({user_tel: user}, {});
            return result;
        }
    );
}